package main

import (
	"fmt"
	"github.com/nsqio/go-nsq"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"
)

type MessageHandler struct {
	Title string
}

func (m *MessageHandler) HandleMessage(msg *nsq.Message) error {
	fmt.Printf("%s recv from %v, msg:%v\n", m.Title, msg.NSQDAddress, string(msg.Body))
	return nil
}

func main() {
	config := nsq.NewConfig()
	config.LookupdPollInterval = 15 * time.Second
	c, err := nsq.NewConsumer("test", "first", config)
	if err != nil {
		log.Fatalf("create consumer err %s\n", err)
	}
	addr := "127.0.0.1:4161" // lookup 发现地址
	consumer := &MessageHandler{Title: "pedro"}
	c.AddHandler(consumer)

	if err = c.ConnectToNSQLookupd(addr); err != nil {
		log.Printf("lookup err %s\n", err)
	}
	s := make(chan os.Signal)
	signal.Notify(s, syscall.SIGINT)
	<-s
}
